<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <!-- <label class="el-form-item-label">id</label>
        <el-input v-model="query.id" clearable placeholder="id" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <label class="el-form-item-label">方案编号</label>
        <el-input v-model="query.schemeNo" clearable placeholder="方案编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">文件名称</label>
        <el-input v-model="query.fileName" clearable placeholder="文件名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker
          v-model="query.createTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="方案编号" prop="schemeNo">
            <el-input v-model="form.schemeNo" style="width: 370px;" />
          </el-form-item>
          <div style="display: none;">
            <el-input v-model="form.fileName" />
            <el-input v-model="form.accessUrl" />
          </div>
          <el-form-item prop="accessUrl" label="上传方案">
            <el-upload
              ref="form"
              accept=".pdf"
              :limit="1"
              :auto-upload="true"
              :before-upload="beforeUpload"
              :headers="authHeaders"
              :on-success="handleSuccess"
              :action="fileUploadApi + '?name=' + form.fileName">
              <div class="eladmin-upload"><i class="el-icon-upload" /> 添加方案</div>
              <div slot="tip" class="el-upload__tip">可上传PDF格式文件，且不超过100M</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" v-if="false" />
        <el-table-column prop="schemeNo" label="方案编号" />
        <el-table-column prop="createUserName" label="创建人" />
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column prop="fileName" label="文件名称" />
        <el-table-column v-if="checkPer(['admin','insuranceScheme:add'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="search" @click="open(baseApi + '/file/' + scope.row.accessUrl)">查看</el-button>
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabledEdit="true"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudInsuranceScheme from '@/api/insurance/scheme'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, createUserId: null, isDeleted: null, createTime: null, schemeNo: null, insuranceType: 1, fileName: null, accessUrl: null, ownerId: null }
export default {
  name: 'InsuranceSchemeAttach',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '附加保险方案', url: 'api/insurance-scheme', idField: 'id', sort: 'id,desc', params: { 'insuranceType': 1 }, crudMethod: { ...crudInsuranceScheme },
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: false
      }
    })
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi'
    ])
  },

  data() {
    return {
      authHeaders: {
        'Authorization': getToken()
      },
      permission: {
        add: ['admin', 'insuranceScheme:add']
      },
      rules: {
        schemeNo: [
          { required: true, message: '方案编号不能为空', trigger: 'blur' }
        ],
        ownerId: [
          { required: true, message: '所属人ID不能为空', trigger: 'blur' }
        ],
        accessUrl: [
          { required: true, message: '请上传方案', trigger: 'change' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'id' },
        { key: 'schemeNo', display_name: '方案编号' },
        { key: 'insuranceType', display_name: '保险方案', value: 1 },
        { key: 'fileName', display_name: '文件名称' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    beforeInit() {
      this.params['insuranceType'] = 1
    },
    beforeUpload(file) {
      this.form.fileName = file.name
      return true
    },
    handleSuccess(response, file, fileList) {
      this.form.accessUrl = response
    },
    open(url) {
      window.open(url, '_blank')
    }
  }
}
</script>

<style scoped>

</style>
